﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProductManager
{
    public partial class ProviderManager : Form
    {
        public ProviderManager()
        {
            InitializeComponent();
        }

        private void SearchProvider()
        {
            DataTable products = DBAccess.Search("Provider", txtSearch.Text, cboSearch.SelectedItem.ToString());
            if (products == null)
            {
                return;
            }
            lvProvider.Items.Clear();
            foreach (DataRow row in products.Rows)
            {
                lvProvider.Items.Add(new ListViewItem(new string[] {
                    row["Name"].ToString(), row["Address"].ToString(),
                    row["Website"].ToString(), row["Rating"].ToString(),
                    row["ID"].ToString() }));
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void ProviderManager_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 0;
            SearchProvider();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchProvider();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddEditProvider frmNew = new AddEditProvider();
            frmNew.InitAdd();
            frmNew.ShowDialog();
            SearchProvider();
        }

        private void btnViewProduct_Click(object sender, EventArgs e)
        {
            if (lvProvider.SelectedItems.Count == 1)
            {
                ProviderProduct frmNew = new ProviderProduct();
                frmNew.Text = "Provider's Product - " + lvProvider.SelectedItems[0].SubItems[0].Text;
                frmNew.ProviderID = lvProvider.SelectedItems[0].SubItems[4].Text;
                frmNew.ShowDialog();
                SearchProvider();
            }
            else
            {
                MessageBox.Show("Please choose one provider!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void lvProvider_DoubleClick(object sender, EventArgs e)
        {
            if (lvProvider.SelectedItems.Count == 1)
            {
                AddEditProvider frmNew = new AddEditProvider();
                if (frmNew.InitEdit(lvProvider.SelectedItems[0].SubItems[4].Text))
                {
                    frmNew.ShowDialog();
                }
            }
        }
    }
}
